﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.ComponentModel;
using System.Drawing;
using Asol.Reporting.Support;
using System.Drawing.Drawing2D;

namespace Asol.Reporting.Green.Components
{
    #region class ButtonFlat
    public class ButtonFlat : Button, IPaintBackground, IPaintLabel
    {
        public ButtonFlat()
        {
            this.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
            this.FlatAppearance.BorderColor = Color.Navy;
            this.FlatAppearance.BorderSize = 1;
            this.SetStyle(ControlStyles.OptimizedDoubleBuffer | ControlStyles.AllPaintingInWmPaint | ControlStyles.UserPaint, true);

            Font df = Control.DefaultFont;
            this.Font = new Font(df.Name, 1.15f * df.Size, FontStyle.Regular);
            this.BackColorStandard = Color.FromArgb(216, 216, 255);
            this.BackColorHotMouse = Color.FromArgb(240, 240, 216);
            this.BackColorPressed = Color.FromArgb(255, 248, 220);
            this.BackColorFocused = Color.FromArgb(216, 216, 255);
            this.BackColorDisabled = Color.FromArgb(190, 200, 210);
        }
        #region Mouse => Status
        protected override void OnMouseEnter(EventArgs e)
        {
            this.MouseIn = true;
            base.OnMouseEnter(e);
        }
        protected override void OnMouseDown(MouseEventArgs mevent)
        {
            this.MouseDn = true;
            base.OnMouseDown(mevent);
        }
        protected override void OnMouseUp(MouseEventArgs mevent)
        {
            this.MouseDn = false;
            base.OnMouseUp(mevent);
        }
        protected override void OnMouseLeave(EventArgs e)
        {
            this.MouseIn = false;
            base.OnMouseEnter(e);
        }
        #endregion
        #region Paint Background, Border
        protected override void OnPaint(PaintEventArgs e)
        {
            e.Graphics.Clear(this.BackColor);
            Painter.PaintButton(this, e.Graphics);
            Painter.PaintLabel(this, e.Graphics);
        }
        #endregion
        #region Public properties
        [Browsable(false)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        protected bool MouseDn { get; set; }
        [Browsable(false)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        protected bool MouseIn { get; set; }

        [Category("Asol")]
        [Description("Barva pozadí buttonu standardní = bez focusu, bez myši, enabled")]
        [DefaultValue(typeof(Color), "216,216,255")]
        [RefreshProperties(System.ComponentModel.RefreshProperties.Repaint)]
        public Color BackColorStandard { get; set; }
        [Category("Asol")]
        [Description("Barva pozadí buttonu hot = bez focusu, s myší, enabled")]
        [DefaultValue(typeof(Color), "240,240,216")]
        public Color BackColorHotMouse { get; set; }
        [Category("Asol")]
        [Description("Barva pozadí buttonu down = stisknutý button")]
        [DefaultValue(typeof(Color), "255,248,220")]
        public Color BackColorPressed { get; set; }
        [Category("Asol")]
        [Description("Barva pozadí buttonu focus = s klávesovým focusem")]
        [DefaultValue(typeof(Color), "216,216,255")]
        public Color BackColorFocused { get; set; }
        [Category("Asol")]
        [Description("Barva pozadí buttonu disabled")]
        [DefaultValue(typeof(Color), "190,200,210")]
        [RefreshProperties(System.ComponentModel.RefreshProperties.Repaint)]
        public Color BackColorDisabled { get; set; }
        #endregion
        #region IPainter, IPaintLabel members
        PaintState IPaintBackground.State
        {
            get
            {
                if (!this.Enabled) return PaintState.Disabled;
                if (this.MouseDn) return PaintState.Pressed;
                if (this.MouseIn && this.Focused) return PaintState.HotMouse | PaintState.Focused;
                if (this.Focused) return PaintState.Focused;
                if (this.MouseIn) return PaintState.HotMouse;
                return PaintState.Standard;
            }
        }
        PaintMode IPaintBackground.PaintMode { get { return PaintMode.GradientHorizontal; } }
        Rectangle IPaintBackground.Bounds { get { return new Rectangle(0, 0, this.Width, this.Height); } }
        Color IPaintBackground.BackColorDisabled { get { return this.BackColorDisabled; } }
        Color IPaintBackground.BackColorStandard { get { return this.BackColorStandard; } }
        Color IPaintBackground.BackColorHotMouse { get { return this.BackColorHotMouse; } }
        Color IPaintBackground.BackColorFocused { get { return this.BackColorFocused; } }
        Color IPaintBackground.BackColorPressed { get { return this.BackColorPressed; } }
        Color IPaintBackground.BorderColor { get { return this.FlatAppearance.BorderColor; } }

        Rectangle IPaintLabel.Bounds { get { return new Rectangle(0, 0, this.Width, this.Height); } }
        int IPaintLabel.Shift { get { return (this.MouseDn ? 1 : (this.MouseIn ? -1 : 0)); } }
        ContentAlignment IPaintLabel.ContentAlignment { get { return System.Drawing.ContentAlignment.MiddleCenter; } }
        string IPaintLabel.Text { get { return this.Text; } }
        Font IPaintLabel.Font { get { return this.Font; } }
        Color IPaintLabel.LabelColor { get { return this.ForeColor; } }
        BorderStyle IPaintLabel.BorderStyle { get { return System.Windows.Forms.BorderStyle.None; } }
        #endregion
    }
    #endregion
    #region class LabelBgr : Label s vykreslovaným pozadím
    /// <summary>
    /// Label s vykreslovaným pozadím
    /// </summary>
    public class LabelBgr : Label, IPaintLabel
    {
        public LabelBgr()
        {
            this.SetStyle(ControlStyles.OptimizedDoubleBuffer | ControlStyles.AllPaintingInWmPaint | ControlStyles.UserPaint, true);

            this.BackColorTop = SystemColors.ControlLight;
            this.BackColorBottom = SystemColors.ControlLight;
        }
        protected override void OnPaint(PaintEventArgs e)
        {
            Painter.PaintLabel(this, e.Graphics);
        }
        protected override void OnPaintBackground(PaintEventArgs e)
        {
            Color color1 = this.BackColorTop;
            Color color2 = this.BackColorBottom;
            Rectangle area = new Rectangle(0, 0, this.Width, this.Height);
            using (System.Drawing.Drawing2D.LinearGradientBrush lgb = new System.Drawing.Drawing2D.LinearGradientBrush(area, color1, color2, 90f))
            {
                e.Graphics.FillRectangle(lgb, area);
            }
            // base.OnPaintBackground(pevent);
        }
        #region IPainter, IPaintLabel members

        Rectangle IPaintLabel.Bounds { get { return new Rectangle(0, 0, this.Width, this.Height); } }
        int IPaintLabel.Shift { get { return 0; } }
        ContentAlignment IPaintLabel.ContentAlignment { get { return this.TextAlign; } }
        string IPaintLabel.Text { get { return this.Text; } }
        Font IPaintLabel.Font { get { return this.Font; } }
        Color IPaintLabel.LabelColor { get { return this.ForeColor; } }
        BorderStyle IPaintLabel.BorderStyle { get { return this.BorderStyle; } }

        #endregion
        #region Public properties
        [Category("Asol")]
        [Description("Barva pozadí na horní hraně")]
        [DefaultValue(typeof(Color), "216,216,255")]
        [RefreshProperties(System.ComponentModel.RefreshProperties.Repaint)]
        public Color BackColorTop { get; set; }
        [Category("Asol")]
        [Description("Barva pozadí na dolní hraně")]
        [DefaultValue(typeof(Color), "240,240,216")]
        public Color BackColorBottom { get; set; }
        #endregion
    }
    #endregion
    #region class QLabelTransparent
    public class QLabelTransparent : Label
    {
        protected override void OnParentChanged(EventArgs e)
        {
            base.OnParentChanged(e);

            if (this.CurrentParent != null)
            {
                this.Bounds = this.VisibleBounds;
            }
            if (this.Parent != null)
            {
                this.Parent.Paint += new PaintEventHandler(Parent_Paint);
            }
            this.CurrentParent = this.Parent;
        }

        void Parent_Paint(object sender, PaintEventArgs e)
        {
            e.Graphics.DrawString(this.Text, this.Font, Brushes.Black, this.VisibleBounds.Location);
        }
        protected override void OnLocationChanged(EventArgs e)
        {
            if (!this.VisibleBoundsChanging)
            {
                base.OnLocationChanged(e);
                this.OriginalBounds = this.Bounds;
                this.VisibleBoundsChange();
            }
        }
        protected override void OnSizeChanged(EventArgs e)
        {
            if (!this.VisibleBoundsChanging)
            {
                base.OnSizeChanged(e);
                this.OriginalBounds = this.Bounds;
                this.VisibleBoundsChange();
            }
        }
        protected void VisibleBoundsChange()
        {
            try
            {
                this.VisibleBoundsChanging = true;
                this.VisibleBounds = this.Bounds;
                if (!this.DesignMode)
                {
                    this.Bounds = new Rectangle(this.Parent.Width + 25, this.Top, this.Width, this.Height);
                }
            }
            finally
            {
                this.VisibleBoundsChanging = false;
            }

        }
        protected Rectangle OriginalBounds { get; set; }
        protected Rectangle VisibleBounds { get; set; }
        protected bool VisibleBoundsChanging { get; set; }
        protected Control CurrentParent { get; set; }
    }
    #endregion
}
